<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Report\Controller;
use Think\Controller;
use User\Api\UserApi;
use Think\Model;
use Think\Upload\Driver\Qiniu\QiniuStorage;

class ReportController extends LoginController {

    public function _initialize(){
        $config = array(
            'accessKey'=>'9_Ejj84S6LT8ICAErGQRPdFbQwKTBJbXsDy8zR-g',
            'secretKey'=>'D_4NmUsFfe9nCZ9c7JbbEhNCpaMfBxaaTsUa6QnZ',
            'bucket'=>'guoran100',
            'domain'=>'7xj380.com1.z0.glb.clouddn.com'
        );
        $this->qiniu = new QiNiuStorage($config);
        parent:: _initialize();
    }
    public function newreport(){
        if(IS_POST){
            $Model = M('Report_list');
            $Model->create();
            $Model->createAt = time();
            $Model->report_uid = REPORTUID;
            $Model->current_day = date("Ymd",time());
            if($Model->add()){
                $this->success("采价已提交,请等待审核结果",'');
            }else{
                $this->error("采价提交失败，请确认",'javascript:history.go(-1)',3);
            }
        }
        $this->title = "采价提交";
        $this->display();
    }
    public function del(){
        $file = trim(I('file'));
        if($file){
            $result = $this->qiniu->del($file);
            if(false === $result){
                $this->error($this->qiniu->errorStr);
            }else{
                $this->success('删除成功');
            }
        }else{
            $this->error('错误的文件名');
        }
    }
    public function dealImage($key){
        $url = $this->qiniu->dealWithType($key, 'img') ;
        redirect($url);
    }
    public function rename(){
        $key = I('get.file');
        $new = I('new_name');
        $result = $this->qiniu->rename($key, $new);
        if(false === $result){
            trace($this->qiniu->error);
            $this->error($this->qiniu->errorStr);
        }else{
            $this->success('改名成功');
        }
    }
    public function uploadOne(){
        if($_POST['data']){
            $fir = strstr($_POST['data'],",");
            $sub = substr($fir,1);
        }
        $file = array(
            'name'=>'file',
            'fileName'=>date("YmdHis",time()).REPORTUID.".png",
            'fileBody'=>base64_decode($sub)
        );
        $config = array();
        $result = $this->qiniu->upload($config, $file);
        if($result){
            $result ="{$this->qiniu->downlink($result['key'])}";
            echo $result;
        }else{
            echo 0;
        }
    }
    public function center(){
        $map['report_uid'] = REPORTUID;
        $count = M('Report_list')->where($map)->count();
        $map['status'] = 2;
        $validcount = M('Report_list')->where($map)->count();
        $salary=M('Report_user')->where(array('id'=>REPORTUID))->getField('reward');
        $this->count = $count;
        $this->validcount = $validcount;
        $this->salary = $salary;
        $this->title = "我的采价";
        $this->display();
    }
    public function history(){
        if(I('accept') == 2){
            $map['status'] = 2;
        }
        $map['report_uid'] = REPORTUID;
        $res = M('report_list')->where($map)->select();
        $this->assign('_list',$res);
        $this->display();
    }

}
